﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace TestOfertanteService
{

    [TestClass]
    public class TestOfertante
    {
        [TestMethod]
        public void TestBusquedaOfertantes()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:17719/Ofertantes.svc/Ofertantes/0,0,0,0,0,0");
            req.Method = "GET";
            HttpWebResponse res = null;
            try
            {
                res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string ofertanteJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                List<Ofertante> resultado = js.Deserialize<List<Ofertante>>(ofertanteJson);
                Assert.AreEqual(8, resultado.Count);

            }
            catch (WebException e)
            {
                HttpWebResponse resError = (HttpWebResponse)e.Response;
                StreamReader readerError = new StreamReader(resError.GetResponseStream());
                string error = readerError.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                string errorMessage = js.Deserialize<string>(error);

                Assert.AreEqual("No se encontro resultados. Modifique los filtros.", errorMessage);
            }
        }

        [TestMethod]
        public void TestListarOfertantes()
        {
            string url = "http://localhost:17719/Ofertantes.svc/Ofertantes/listar/1";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            HttpWebResponse res = null;

            res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ofertanteJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Ofertante> resultado = js.Deserialize<List<Ofertante>>(ofertanteJson);
            
            Assert.AreEqual(8,resultado.Count());
        }

        [TestMethod]
        public void TestObtenerOfertante()
        {
            string url = "http://localhost:17719/Ofertantes.svc/Ofertantes/3";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ofertanteJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Ofertante ofertanteObtenido = js.Deserialize<Ofertante>(ofertanteJson);

            Assert.AreEqual("Carlos Ormeño Avalos", ofertanteObtenido.RazonSocial);
        }

        [TestMethod]
        public void TestModificarOfertante()
        {

            string url = "http://localhost:17719/Ofertantes.svc/Ofertantes/3";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ofertanteJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Ofertante ofertanteObtenido = js.Deserialize<Ofertante>(ofertanteJson);

            ofertanteObtenido.StdRegistro = 1;
            ofertanteObtenido.DscRechazo = "Aprobado por el administrador";
            StringBuilder jsonLista = new StringBuilder();
            js = new JavaScriptSerializer();
            js.Serialize(ofertanteObtenido, jsonLista);
            string postdata = jsonLista.ToString();
            url = "http://localhost:17719/Ofertantes.svc/Ofertantes";

            byte[] data = Encoding.UTF8.GetBytes(postdata);
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "PUT";
            req.ContentLength = data.Length;
            req.ContentType = "application/json";
            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            res = (HttpWebResponse)req.GetResponse();
            reader = new StreamReader(res.GetResponseStream());

            ofertanteJson = reader.ReadToEnd();
            js = new JavaScriptSerializer();
            ofertanteObtenido = new Ofertante();
            ofertanteObtenido = js.Deserialize<Ofertante>(ofertanteJson);

            Assert.AreEqual("Aprobado por el administrador", ofertanteObtenido.DscRechazo);
        }

        [TestMethod]
        public void crearOfertante()
        {
            string postdata = "{\"CodOfertante\":\"9" +
                              "\",\"TipoDocOfertante\":\"" +
                               "\",\"NumDocOfertante\":\"" +
                               "\",\"EmailOfertante\":\"" +
                               "\",\"UsuarioOfertante\":\"" +
                                "\",\"ClaveOfertante\":\"" +
                               "\",\"RazonSocial\":\"" +
                               "\",\"DepOfertante\":\"" +
                               "\",\"ProvOfertante\":\"" +
                              "\",\"DistOfertante\":\"" +
                               "\",\"FlgMembresia\":\"" +
                               "\",\"NumTelefOfertante\":\"" +
                               "\"}";


            byte[] data = Encoding.UTF8.GetBytes(postdata);
            string url = "http://localhost:17719/Ofertantes.svc/Ofertantes";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentLength = data.Length;
            req.ContentType = "application/json";
            var reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);

            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ofertanteJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            Ofertante resultado = js.Deserialize<Ofertante>(ofertanteJson);

            Assert.AreEqual(5, resultado.CodOfertante);

        }
    }
}
